<?php


// &__kds_flag=post
$post = [
    "keydatas_password"=>"password",
    "post_title"=>"", //标题
    "post_content"=>"", //内容
    "post_excerpt"=>"", //描述
    "post_type" =>"portfolio", //
    "post_status" =>"publish",
    "comment_status" => "closed",
    "post_category"=>"", 
    "__kds_feature_url"=>"",//缩略图
    "post_thumbnail"=>"",//缩略图2
    "__kdsExt__aviaLayoutBuilder_active"=>"active",
];


/* 远程函数 */
function add_attachment($url)
{
    $attach_id = 0;
    $upload_dir = wp_upload_dir();
    $suffix = "jpg";
    $filename = md5($url) . "." . $suffix;
    if (wp_mkdir_p($upload_dir['path'])) {
        $file = $upload_dir['path'] . '/' . $filename;
    } else {
        $file = $upload_dir['basedir'] . '/' . $filename;
    }
    $image_data = file_get_contents($url);
    file_put_contents($file, $image_data);
    if (file_exists($file)) {
        $wp_filetype = wp_check_filetype($filename, null);
        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => sanitize_file_name($filename),
            'post_content' => '',
            'post_status' => 'inherit',
            'guid' => $url
        );
        $attach_id = wp_insert_attachment($attachment, $file);
        wp_update_attachment_metadata($attach_id, wp_generate_attachment_metadata($attach_id,$file));
    }
    return $attach_id;
}
